﻿using EasyDDD.Domain;
using Microsoft.EntityFrameworkCore;

namespace EasyDDD.Infrastructure.EFCore.Records
{
    public abstract class Records<TContext, TEntiy> : IRecords<TEntiy>
        where TContext : EFRecordsContext<TContext>
        where TEntiy : class, IEntity
    {
        protected virtual TContext DbContext { get; set; }

        protected Records(TContext DbContext)
        {
            this.DbContext = DbContext;
        }

        public IQueryable<TEntiy> AsQueryable(Guid? tenantId = null)
        {
            if (tenantId == null) tenantId = Guid.Empty;
            return DbContext.Set<TEntiy>().AsNoTracking()
                .Where(e => e.IsDeleted == false && e.TenantId == tenantId)
                .AsQueryable();
        }

        public IQueryable<TEntiy> AsRawQueryable()
        {
            return DbContext.Set<TEntiy>().AsNoTracking().AsQueryable();
        }
    }
}
